import * as THREE from "three";

// 初始化地图浮动效果
function initMapFloat(camera, mapModel, provinceModel) {
  // 节流函数
  const throttleChange = throttle(rayMonitor, 5, false);
  // 监听鼠标移动事件
  addEventListener("mousemove", (e) => {
    throttleChange(e, camera, mapModel, provinceModel);
  });
}

// 节流函数
function throttle(func, limit) {
  let inThrottle;
  return function () {
    const args = arguments;
    const context = this;
    if (!inThrottle) {
      func.apply(context, args);
      inThrottle = true;
      setTimeout(() => (inThrottle = false), limit);
    }
  };
}

// 射线监测当前鼠标移动点是否有地图模型
function rayMonitor(e, camera, mapModel, provinceModel) {
  const px = e.offsetX;
  const py = e.offsetY;
  // 屏幕坐标转为标准设备坐标
  const x = (px / window.innerWidth) * 2 - 1;
  const y = -(py / window.innerHeight) * 2 + 1;
  // 创建射线
  const raycaster = new THREE.Raycaster();
  // 设置射线参数
  raycaster.setFromCamera(new THREE.Vector2(x, y), camera);
  // 全国地图浮动
  if (mapModel.visible) {
    if (mapModel.children.length <= 1) return;
    // 射线交叉计算拾取模型
    let intersects = raycaster.intersectObjects(mapModel.children);
    // 检测结果过滤掉光圈
    intersects = intersects.filter(function (intersect) {
      return intersect.object.name !== "光圈";
    });
    // 先停止所有动画
    mapModel.children.forEach((item) => {
      if (item.animationTimer) {
        clearInterval(item.animationTimer);
        item.animationTimer = null;
      }
      item.animationActive = false;
    });
    // 射线选中模型时
    if (intersects.length > 0) {
      const parent = intersects[0].object.parent;
      // 射线第一个拾取中的模型
      const obj = parent.name && parent.name !== "省份边线" ? parent : parent.parent;
      obj.animationActive = true;
      mapModel.children.map((item) => {
        if (item.animationActive) {
          // 添加一个白色的发射光呈现高亮效果
          item.children[1].material[0].emissive = new THREE.Color("#fff");
          item.animationTimer = setInterval(() => {
            if (item.position.z <= 4) {
              item.position.z += 0.3;
            } else {
              clearInterval(item.animationTimer);
              item.animationTimer = null;
            }
          }, 5);
        } else {
          // 默认重置为黑色的发射光
          item.children[1].material[0].emissive = new THREE.Color("#000");
          item.animationTimer = setInterval(() => {
            if (item.position.z > 0) {
              item.position.z -= 0.1;
            } else {
              clearInterval(item.animationTimer);
              item.animationTimer = null;
            }
          }, 5);
        }
      });
    } else {
      mapModel.children.forEach((item) => {
        item.animationTimer = setInterval(() => {
          // 默认重置为黑色的发射光
          item.children[1].material[0].emissive = new THREE.Color("#000");
          if (item.position.z > 0) {
            item.position.z -= 0.1;
          } else {
            clearInterval(item.animationTimer);
            item.animationTimer = null;
          }
        }, 5);
      });
    }
  }
  // 省份地图浮动
  else if (provinceModel.visible) {
    if (provinceModel.children.length <= 1) return;

    // 射线交叉计算拾取模型
    let intersects = raycaster.intersectObjects(provinceModel.children);
    // 检测结果过滤边线
    intersects = intersects.filter(function (intersect) {
      return intersect.object.name !== "边线";
    });
    // 先停止所有动画
    provinceModel.children.forEach((item) => {
      if (item.userData.animationTimer) {
        clearInterval(item.userData.animationTimer);
        item.userData.animationTimer = null;
      }
      item.userData.animationActive = false;
    });
    // 射线选中模型时
    if (intersects.length > 0) {
      const obj = intersects[0].object.parent;
      obj.userData.animationActive = true;
      provinceModel.children.map((item) => {
        if (item.userData.animationActive) {
          // 添加一个白色的发射光呈现高亮效果
          item.traverse((obj) => {
            if (obj.type === "mesh") {
              obj.material[0].emissive = new THREE.Color("#fff");
            }
          });
          item.userData.animationTimer = setInterval(() => {
            if (item.position.z <= 4) {
              item.position.z += 0.3;
            } else {
              clearInterval(item.userData.animationTimer);
              item.userData.animationTimer = null;
            }
          }, 5);
        } else {
          // 默认重置为黑色的发射光
          item.traverse((obj) => {
            if (obj.type === "mesh") {
              obj.material[0].emissive = new THREE.Color("#000");
            }
          });
          item.userData.animationTimer = setInterval(() => {
            if (item.position.z > 0) {
              item.position.z -= 0.1;
            } else {
              clearInterval(item.userData.animationTimer);
              item.userData.animationTimer = null;
            }
          }, 5);
        }
      });
    } else {
      provinceModel.children.forEach((item) => {
        item.userData.animationTimer = setInterval(() => {
          // 默认重置为黑色的发射光
          item.traverse((obj) => {
            if (obj.type === "mesh") {
              obj.material[0].emissive = new THREE.Color("#000");
            }
          });
          if (item.position.z > 0) {
            item.position.z -= 0.1;
          } else {
            clearInterval(item.userData.animationTimer);
            item.userData.animationTimer = null;
          }
        }, 5);
      });
    }
  }
}

export { initMapFloat };
